package com.yq.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.reggie.pojo.Employee;
import com.yq.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @return
     */
    @PostMapping("/login")
    public Result login(HttpServletRequest request, @RequestBody Employee employee){
            //将密码转换为 md5加密码
            String password = employee.getPassword();
            password = DigestUtils.md5DigestAsHex(password.toString().getBytes());
            //根据username查询数据库
            LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Employee::getUsername,employee.getUsername());
            Employee emp = null;
            emp = employeeService.getOne(wrapper);
            //根据查询结果发出响应
            if(emp==null){
                return Result.fail("用户名不存在");
            }
            if(emp.getPassword().equals(password)) {
                request.getSession().setAttribute("employee",emp.getId());
                return Result.success(emp);
            }else{
                return Result.fail("密码错误");
            }
    }
    /**
     * 员工退出
     */
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request){
        //清除localStorage
        request.removeAttribute("userInfo");
        return Result.success("");
    }
    /**
     * 添加员工
     */
    @PostMapping
    public Result addEmployee(HttpServletRequest request,@RequestBody Employee employee){
        //完善employee
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        LocalDateTime now = LocalDateTime.now();
/*
        employee.setCreateTime(now);
        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateTime(now);
*/

        //执行sql
        employeeService.save(employee);
        return Result.success(employee);
    }

    /**
     * 分页展示或按名称分页展示
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result selectPage(Integer page,Integer pageSize,String name){
        //分页查询
        IPage<Employee> apage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> wrapper= new LambdaQueryWrapper<>();
        wrapper.like(name==null?false:true,Employee::getName,name);
        wrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(apage,wrapper);

        return Result.success(apage);
    }
    /**
     * 修改员工信息（2修改）和状态
     */
    @PutMapping
    public Result update(HttpServletRequest request,@RequestBody Employee employee){
/*
        employee.setUpdateUser((Long)request.getSession().getAttribute("employee"));
        employee.setUpdateTime(LocalDateTime.now());
*/
        boolean result = employeeService.updateById(employee);
        return Result.success(employee);
    }
    /**
     * 修改员工信息（1回显）
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id){
        //根据id查询
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getId,id);
        Employee one = employeeService.getOne(wrapper);

        return Result.success(one);
    }
}
